﻿using Newtonsoft.Json;
using QG.FrontDesk.BLL.IService;
using QG.FrontDesk.com.Attbutes;
using QG.FrontDesk.com.Models;
using QG.FrontDesk.Core;
using QG.FrontDesk.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace QG.FrontDesk.com.Controllers
{
    public class HomeController : Controller
    {
        private IClassifyinfoService _classifyinfoService;

        private IPositionInfoService _positionInfoService;


        public HomeController(IClassifyinfoService classifyinfoService, IPositionInfoService positionInfoService)
        {
            _classifyinfoService = classifyinfoService;
            _positionInfoService = positionInfoService;
        }

        public ActionResult Error()
        {
            return View();
        }

        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns> 
        public ActionResult Index()
        {
            DataContext<Company>.context.model = null;
            DataContext<UserInfo>.context.model = null;
            //获取存在的cookie
            //再次浏览器判断是否有值
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
            if (cookie != null)
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string userData = ticket.UserData;
                if (ticket.Name == "求职")
                {                 
                    DataContext<UserInfo>.context.model= JsonConvert.DeserializeObject<UserInfo>(userData);
                }
                else if (ticket.Name == "公司")
                {                    
                    DataContext<Company>.context.model = JsonConvert.DeserializeObject<Company>(userData);
                }                
            }
            //获取菜单分类数据集合
            List<Classifyinfo> classifyinfos = _classifyinfoService.GetClassifyinfos();
            //获取所有职位数据集合
            List<PostName> postNames = _classifyinfoService.GetPostNames();
            //获取所有职位信息
            List<PositionInfo> positionInfos = _positionInfoService.GetPositionInfos(null, null, null, null);
            //展示菜单分类数据到页面
            ViewBag.classifyinfos = classifyinfos;
            ViewBag.postNames = postNames;
            ViewBag.positionInfos = positionInfos;
            return View();
        }

        /// <summary>
        /// 关于页面
        /// </summary>
        /// <returns></returns>
        public ActionResult About()
        {
            DataContext<Company>.context.model = null;
            DataContext<UserInfo>.context.model = null;
            //获取存在的cookie
            //再次浏览器判断是否有值
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
            if (cookie != null)
            {
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                string userData = ticket.UserData;
                if (ticket.Name == "求职")
                {
                    DataContext<UserInfo>.context.model = JsonConvert.DeserializeObject<UserInfo>(userData);
                }
                else if (ticket.Name == "公司")
                {
                    DataContext<Company>.context.model = JsonConvert.DeserializeObject<Company>(userData);
                }
            }
            return View();
        }
        /// <summary>
        /// 找回密码页面
        /// </summary>
        /// <returns></returns>
        [PermissionAttribute]
        public ActionResult Reset()
        {
            return View();
        }


        /// <summary>
        /// 发送邮箱验证
        /// </summary>
        /// <returns></returns>        
        public bool SendEmailEmail(string email)
        {
            var code = Guid.NewGuid().ToString();//生成验证码
            Session["code"] = code;//存储验证码
            var resultUrl = "https://localhost:44319/Company/GetCodeResult?code=";//返回验证路径
            var mailTo = email;

            var mailSubject = DataContext<UserInfo>.context.model.Email + "密码找回";//邮件标题
            var mailContent = "点击链接 <a href ='" + resultUrl + code + "'target='_self' >找回" + DataContext<UserInfo>.context.model.UserPwd + "邮箱账号</a > </br>如点击跳转无效,请邮件打开链接";//邮件内容

            return EmailHelper.SendEmail(mailTo, mailSubject, mailContent);
        }

        public ActionResult Thesecondstep(string name)
        {
            DataContext<UserInfo>.context.model.UserName = name;
            string email = DataContext<UserInfo>.context.model.Email;//发送验证信息的邮箱            
            ViewBag.Two = SendEmailEmail(email) ? email : null;
            return RedirectToAction("Reset", "Home");
        }

    }
}